package com.yt.order.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yt.order.models.bo.WorkOrderInfoSaveBO;
import com.yt.order.models.dto.WorkOrderInfoDTO;
import com.yt.user.models.dto.CustomerInfoDTO;
import com.yt.user.models.service.ICustomerInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.order.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.order.mapper.DispatchRuleConfigMapper;
import com.yt.order.models.service.IDispatchRuleConfigService;
import com.yt.order.models.bo.DispatchRuleConfigSaveBO;
import com.yt.order.models.dto.DispatchRuleConfigDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * 派单规则信息表控制器
 *
 * @author zhou
 * @Date 2025-09-14 10:05:55
 */
@DubboService
public class DispatchRuleConfigServiceImpl extends ServiceImpl<DispatchRuleConfigMapper, DispatchRuleConfigDTO> implements IDispatchRuleConfigService{

    private static final Logger log = LoggerFactory.getLogger(DispatchRuleConfigServiceImpl.class);
    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private IDispatchRuleConfigService dispatchRuleConfigService;
    @DubboReference
    private ICustomerInfoService customerInfoService;

    @Override
    public boolean add(DispatchRuleConfigSaveBO saveBO) {
        DispatchRuleConfigDTO dto = new DispatchRuleConfigDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<DispatchRuleConfigDTO> queryRuleConfig(WorkOrderInfoDTO workOrderInfoDTO) {
        List<DispatchRuleConfigDTO> dtoList=new ArrayList<>();
        if(workOrderInfoDTO.getCustomerId()!=null){
            Wrapper<CustomerInfoDTO> wp = new EntityWrapper<>();
            wp.eq("id",workOrderInfoDTO.getCustomerId());
            CustomerInfoDTO customerInfoDTO=customerInfoService.selectOne(wp);
            //如果客户是企业级客户（1），则使用VIP类型（2）规则
            if(customerInfoDTO.getType()==1){
                Wrapper<DispatchRuleConfigDTO> wrapper = new EntityWrapper<>();
                wrapper.eq("type",2);
                wrapper.eq("status",1);
                wrapper.orderBy("priority",false);
                dtoList =dispatchRuleConfigService.selectList(wrapper);
                return dtoList;
            }
            //优先级高的工单
            else if(workOrderInfoDTO.getPriority()==3){
                Wrapper<DispatchRuleConfigDTO> wrapper = new EntityWrapper<>();
                wrapper.eq("type",1);
                wrapper.eq("status",1);
                wrapper.orderBy("priority");
                dtoList =dispatchRuleConfigService.selectList(wrapper);
                return dtoList;
            }
            else{
                Wrapper<DispatchRuleConfigDTO> wrapper = new EntityWrapper<>();
                wrapper.eq("type",0);
                wrapper.eq("status",1);
                wrapper.orderBy("priority");
                dtoList =dispatchRuleConfigService.selectList(wrapper);
                return dtoList;
            }
        }
        //如果优先级是高且是自建工单
        if(workOrderInfoDTO.getPriority()==3){
            Wrapper<DispatchRuleConfigDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("type",1);
            wrapper.eq("status",1);
            wrapper.orderBy("priority");
            dtoList =dispatchRuleConfigService.selectList(wrapper);
        }
        //其他使用普通类型规则
        else {
            Wrapper<DispatchRuleConfigDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("type",0);
            wrapper.eq("status",1);
            wrapper.orderBy("priority");
            dtoList =dispatchRuleConfigService.selectList(wrapper);
        }
        return dtoList;
    }
}
